/**
 * 各种类型转换
 * @param bArr byte[] in Java
 */
function bArr2str(bArr, length = 0) {
    const JavaString = Java.classFactory.use("java.lang.String")
    if (length > 0) {
        return JavaString.$new(bArr, 0, length).toString()
    }
    return JavaString.$new(bArr).toString()
}

function str2bArr(str) {
    const JavaString = Java.classFactory.use("java.lang.String")
    return JavaString.$new(str).getBytes()
}
function byte_to_ArrayBuffer(bytes) {
    var size = bytes.length;
    var tmparray = [];
    for (var i = 0; i < size; i++) {
        var val = bytes[i];
        if (val < 0) {
            val += 256;
        }
        tmparray[i] = val
    }
    return tmparray;
}
function bArr2hexdump(bArr, length = 0) {
    let size = length > 0 ? length : bArr.length
    var buf = Memory.alloc(size);
    Memory.writeByteArray(buf, byte_to_ArrayBuffer(bArr));
    console.log(
        hexdump(buf, {
            offset: 0,
            length: size,
            header: false,
            ansi: true
        }));
}

export { bArr2str, str2bArr, bArr2hexdump }